import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';

import { Service } from '../../../server/server';
import { InitService } from '../../../public/serve/init/init.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';

import { appProductUrl } from '../../product.url';
import { product } from '../../product.data';

import { ProductIndexingTaskContentTableComponent } from './table/product-indexing-task-content-table.component';
declare var $: any;

@Component({
	selector: 'app-product-indexing-task-content',
	templateUrl: './product-indexing-task-content.component.html',
	styleUrls: ['./product-indexing-task-content.component.css']
})
export class ProductIndexingTaskContentComponent implements OnInit {

	@ViewChild("table") table: ProductIndexingTaskContentTableComponent;
	isActive = this.server.isActive;
	taskId: string;
	parentId: string;
	child:boolean;
	completeStatus: string;
	taskType: any = '0';
	projectId:any = '';
    right: any = {
        "taskPermissionContent":{},
        "productTaskContent": {},
		"internalTaskContent": {},
		"themeTaskContent":{}
    };

	constructor(
		private activatedRoute: ActivatedRoute,
		public initService: InitService,
		public removeTableService: RemoveTableService,
		public request: CudTableService,
		public server: Service,
		private hintsService: HintsService
	) { 
		this.right = $.extend(this.right, server.getRight());
	}

	ngOnInit() {
		this.activatedRoute.queryParams.subscribe((p: Params) => {
			this.taskId = String(p['taskId']);
			this.parentId = String(p['parentId']);
			this.completeStatus = String(p['completeStatus']);
			this.child = Boolean(Number(p['child']));
			this.taskType = String(p['taskType']);
			this.projectId = String(p['projectId']);

			//权限
            if(this.taskType == '1'){
                //内文任务
                this.right['taskPermissionContent'] = this.right['internalTaskContent'];
            }else if(this.taskType == '0'){
                //产品任务
                this.right['taskPermissionContent'] = this.right['productTaskContent'];
			}else if(this.taskType == '2'){
                //主题词任务
				this.right['taskPermissionContent'] = this.right['themeTaskContent'];
			}
		});

		this.server.eventEmit.subscribe((value: any) => {
			this.isActive = value;
		});
	}

	/**
	* 分页
	* @param pagination 
	*/
	_pagination(pagination: Array<any>) {
		product.pagination.pageNumber = pagination[0].pageNumber;
		product.pagination.pageSize = pagination[0].pageSize;
		product.pagination.naturalNum = (product.pagination.pageNumber - 1) * product.pagination.pageSize;
		this.table.getTaskProductList();
	}

	/**
	 * 搜索
	 * @param val
	 */
	_retrieval(val: string) {
		product.retrieval.queryCriteria = val.trim();
		this.table.getTaskProductList();
	}

	/**
	 * 设为参考标引
	 * TODO:接口还未完成
	 */
	setRefIndexingData() {
		const data = this.removeTableService._AllId('productId');
		for (let item of data['productId']) {
			this.setRefIndexing(item);
		}
	}

	async setRefIndexing(productId) {
		// const params = this.getParams(productId);
		// await this.request.postRequest(appProductUrl.getTaskProductList, params).then((res: any) => {
		// 	if (res.result_code === '0000') {

		// 	} else {
		// 		this.hintsService.messages(res.message, res.result_code);
		// 	}
		// 	return;
		// }).catch((err: any) => {
		// 	this.hintsService.messages("error", "error");
		// 	return;
		// });
	}

	getParams(productId) {
		return {
			token: product.retrieval.token,
			id: '',
			productId: product,
			taskId: this.table.taskId,
			status: '',
			checkStatus: '',
			refIndexing: 1,
			updateBy: ''
		}
	}


}
